package com.ruoyi.wb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.wb.domain.WbPlan;
import com.ruoyi.wb.domain.WbPlanClassifyYear;
import com.ruoyi.wb.domain.WbPlanClassifyYearContent;
import com.ruoyi.wb.domain.WbPlanClassifyYearContentThree;
import com.ruoyi.wb.enmus.RoleNameEnums;
import com.ruoyi.wb.mapper.WbPlanClassifyYearContentMapper;
import com.ruoyi.wb.mapper.WbPlanClassifyYearContentThreeMapper;
import com.ruoyi.wb.mapper.WbPlanClassifyYearMapper;
import com.ruoyi.wb.mapper.WbPlanMapper;
import com.ruoyi.wb.service.IWbPlanClassifyYearContentService;
import com.ruoyi.wb.service.IWbPlanClassifyYearService;
import com.ruoyi.wb.vo.WbPlanClassifyYearContentThreeVo;
import com.ruoyi.wb.vo.WbPlanClassifyYearContentVo;
import com.ruoyi.wb.vo.WbPlanClassifyYearVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 维保年度类目列Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-15
 */
@Service
public class WbPlanClassifyYearServiceImpl implements IWbPlanClassifyYearService 
{
    @Autowired
    private WbPlanClassifyYearMapper wbPlanClassifyYearMapper;

    @Autowired
    private WbPlanClassifyYearContentMapper wbPlanClassifyYearContentMapper;

    @Autowired
    private WbPlanClassifyYearContentThreeMapper wbPlanClassifyYearContentThreeMapper;

    @Autowired
    private IWbPlanClassifyYearContentService wbPlanClassifyYearContentService;

   @Autowired
   private WbPlanMapper wbPlanMapper;

    /**
     * 查询维保年度类目列
     * 
     * @param id 维保年度类目列主键
     * @return 维保年度类目列
     */
    @Override
    public WbPlanClassifyYear selectWbPlanClassifyYearById(Long id)
    {
        return wbPlanClassifyYearMapper.selectWbPlanClassifyYearById(id);
    }

    /**
     * 查询维保年度类目列列表
     * 
     * @param wbPlanClassifyYear 维保年度类目列
     * @return 维保年度类目列
     */
    @Override
    public List<WbPlanClassifyYear> selectWbPlanClassifyYearList(WbPlanClassifyYear wbPlanClassifyYear)
    {
        return wbPlanClassifyYearMapper.selectWbPlanClassifyYearList(wbPlanClassifyYear);
    }

    /**
     * 新增维保年度类目列
     * 
     * @param wbPlanClassifyYear 维保年度类目列
     * @return 结果
     */
    @Override
    public int insertWbPlanClassifyYear(WbPlanClassifyYear wbPlanClassifyYear)
    {
        wbPlanClassifyYear.setCreateTime(DateUtils.getNowDate());
        return wbPlanClassifyYearMapper.insertWbPlanClassifyYear(wbPlanClassifyYear);
    }

    /**
     * 修改维保年度类目列
     * 
     * @param wbPlanClassifyYear 维保年度类目列
     * @return 结果
     */
    @Override
    public int updateWbPlanClassifyYear(WbPlanClassifyYear wbPlanClassifyYear)
    {
        wbPlanClassifyYear.setUpdateTime(DateUtils.getNowDate());
        return wbPlanClassifyYearMapper.updateWbPlanClassifyYear(wbPlanClassifyYear);
    }

    /**
     * 批量删除维保年度类目列
     * 
     * @param ids 需要删除的维保年度类目列主键
     * @return 结果
     */
    @Override
    public int deleteWbPlanClassifyYearByIds(Long[] ids)
    {
        return wbPlanClassifyYearMapper.deleteWbPlanClassifyYearByIds(ids);
    }

    /**
     * 删除维保年度类目列信息
     * 
     * @param id 维保年度类目列主键
     * @return 结果
     */
    @Override
    public int deleteWbPlanClassifyYearById(Long id)
    {
        return wbPlanClassifyYearMapper.deleteWbPlanClassifyYearById(id);
    }

    /**
     * 根据计划id查询所有年度目录
     * @param planId
     * @return
     */
    @Override
    public List<WbPlanClassifyYearVo> getCatalogueYearItemByPlanId(Long planId) {
        List<WbPlanClassifyYearVo> wbPlanClassifyYearVos = new ArrayList<>();
        WbPlanClassifyYear wbPlanClassifyYear = new WbPlanClassifyYear();
        wbPlanClassifyYear.setPlanId(planId);
        //一级目录
        List<WbPlanClassifyYear> wbPlanClassifyYears = wbPlanClassifyYearMapper.selectWbPlanClassifyYearList(wbPlanClassifyYear);
        //校验
        if (CollectionUtils.isEmpty(wbPlanClassifyYears)){
            return wbPlanClassifyYearVos;
        }
        //转换一级返回
        wbPlanClassifyYearVos = BeanUtil.copyToList(wbPlanClassifyYears, WbPlanClassifyYearVo.class);
        //校验
        List<Long> WbPlanClassifyYearIds = wbPlanClassifyYears.stream().map(WbPlanClassifyYear::getId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(WbPlanClassifyYearIds)){
            return wbPlanClassifyYearVos;
        }
        //查询二级目录
        List<WbPlanClassifyYearContent> wbPlanClassifyYearContents=wbPlanClassifyYearContentMapper.selectWbPlanClassifyYearContentByClassifyYearIds(WbPlanClassifyYearIds);
        if (CollectionUtils.isEmpty(wbPlanClassifyYearContents)){
            return wbPlanClassifyYearVos;
        }
        //转换为二级返回
        List<WbPlanClassifyYearContentVo> wbPlanClassifyYearContentVos = BeanUtil.copyToList(wbPlanClassifyYearContents, WbPlanClassifyYearContentVo.class);


        List<Long> classifyYearContentIds = wbPlanClassifyYearContentVos.stream().map(WbPlanClassifyYearContentVo::getClassifyYearContentId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(classifyYearContentIds)){
            return wbPlanClassifyYearVos;
        }
        //查询三级目录
       List<WbPlanClassifyYearContentThree> wbPlanClassifyYearContentThrees =wbPlanClassifyYearContentThreeMapper.selectWbPlanClassifyYearContentThreeByClassifyYearContentIds(classifyYearContentIds);

        if (CollectionUtils.isEmpty(wbPlanClassifyYearContentThrees)){
            //二级装入一级
            Map<Long, List<WbPlanClassifyYearContentVo>> mapTwo = new HashMap<>();
            wbPlanClassifyYearContentVos.forEach(item->{
                if (mapTwo.containsKey(item.getClassifyYearId())){
                    mapTwo.get(item.getClassifyYearId()).add(item);
                }else {
                    List<WbPlanClassifyYearContentVo> itemWbPlanClassifyYearContentVos = new ArrayList<>();
                    itemWbPlanClassifyYearContentVos.add(item);
                    mapTwo.put(item.getClassifyYearId(),itemWbPlanClassifyYearContentVos);
                }
            });
            wbPlanClassifyYearVos.forEach(item->{
                if (mapTwo.containsKey(item.getId())){
                    item.setWbPlanClassifyYearContentVos(mapTwo.get(item.getId()));
                }
            });

            return wbPlanClassifyYearVos;
        }
        //转换为三级返回
        List<WbPlanClassifyYearContentThreeVo> wbPlanClassifyYearContentThreeVos = BeanUtil.copyToList(wbPlanClassifyYearContentThrees, WbPlanClassifyYearContentThreeVo.class);
        //三级装入二级
        Map<Long, List<WbPlanClassifyYearContentThreeVo>> mapThree = new HashMap<>();
        wbPlanClassifyYearContentThreeVos.forEach(item->{
            if (mapThree.containsKey(item.getClassifyYearContentId())){
                mapThree.get(item.getClassifyYearContentId()).add(item);
            }else {
                List<WbPlanClassifyYearContentThreeVo> itemWbPlanClassifyYearContentThreeVos = new ArrayList<>();
                itemWbPlanClassifyYearContentThreeVos.add(item);
                mapThree.put(item.getClassifyYearContentId(),itemWbPlanClassifyYearContentThreeVos);
            }
        });
        wbPlanClassifyYearContentVos.forEach(item->{
            if (mapThree.containsKey(item.getClassifyYearContentId())){
                item.setWbPlanClassifyYearContentThreeVos(mapThree.get(item.getClassifyYearContentId()));
            }
        });
        //二级装入一级
        Map<Long, List<WbPlanClassifyYearContentVo>> mapTwo = new HashMap<>();
        wbPlanClassifyYearContentVos.forEach(item->{
            if (mapTwo.containsKey(item.getClassifyYearId())){
                mapTwo.get(item.getClassifyYearId()).add(item);
            }else {
                List<WbPlanClassifyYearContentVo> itemWbPlanClassifyYearContentVos = new ArrayList<>();
                itemWbPlanClassifyYearContentVos.add(item);
                mapTwo.put(item.getClassifyYearId(),itemWbPlanClassifyYearContentVos);
            }
        });
        wbPlanClassifyYearVos.forEach(item->{
            if (mapTwo.containsKey(item.getId())){
                item.setWbPlanClassifyYearContentVos(mapTwo.get(item.getId()));
            }
        });
        return wbPlanClassifyYearVos;
    }

    @Override
    public int batchAdd(List<WbPlanClassifyYear> wbPlanClassifyYears) {
        return wbPlanClassifyYearMapper.batchAdd(wbPlanClassifyYears);
    }

    /**
     * 批量查询所有年检目录
     * @param wbPlanClassifyYear
     * @return
     */
    @Override
    public List<WbPlanClassifyYearVo> allList(WbPlanClassifyYear wbPlanClassifyYear) {
        {
            List<WbPlanClassifyYearVo> wbPlanClassifyYearVos = new ArrayList<>();
            SysUser user = SecurityUtils.getLoginUser().getUser();
            Long userId = user.getUserId();
            List<SysRole> roles = user.getRoles();
            if (org.springframework.util.CollectionUtils.isEmpty(roles)){
                throw new RuntimeException("该用户没有角色信息");
            }
            WbPlan wbPlan = new WbPlan();
            for (SysRole role:roles) {
                if (role.getRoleName().equals(RoleNameEnums.MAINTAIN_STAFF.getDescribe())){//维保人员
                    wbPlan.setOperaterId(userId);
                }else if (role.getRoleName().equals(RoleNameEnums.ENGINEER.getDescribe())){ //工程师
                    wbPlan.setFireFightingId(userId);
                    wbPlan.setEngineerId(userId);
                }
            }

            List<WbPlan> wbPlans = wbPlanMapper.selectWbPlanListByRole(wbPlan);
            if (CollectionUtils.isEmpty(wbPlans)){
                return wbPlanClassifyYearVos;
            }
            List<Long> planIds = wbPlans.stream().map(WbPlan::getId).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(planIds)){
                return wbPlanClassifyYearVos;
            }



            //一级目录
            List<WbPlanClassifyYear> wbPlanClassifyYears =wbPlanClassifyYearMapper.selectWbPlanClassifyYearListByPlanIds(planIds);
            //校验
            if (CollectionUtils.isEmpty(wbPlanClassifyYears)){
                return wbPlanClassifyYearVos;
            }
            //转换一级返回
            wbPlanClassifyYearVos = BeanUtil.copyToList(wbPlanClassifyYears, WbPlanClassifyYearVo.class);
            //校验
            List<Long> WbPlanClassifyYearIds = wbPlanClassifyYears.stream().map(WbPlanClassifyYear::getId).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(WbPlanClassifyYearIds)){
                return wbPlanClassifyYearVos;
            }
            //查询二级目录
            List<WbPlanClassifyYearContent> wbPlanClassifyYearContents=wbPlanClassifyYearContentMapper.selectWbPlanClassifyYearContentByClassifyYearIds(WbPlanClassifyYearIds);
            if (CollectionUtils.isEmpty(wbPlanClassifyYearContents)){
                return wbPlanClassifyYearVos;
            }
            //转换为二级返回
            List<WbPlanClassifyYearContentVo> wbPlanClassifyYearContentVos = BeanUtil.copyToList(wbPlanClassifyYearContents, WbPlanClassifyYearContentVo.class);


            List<Long> classifyYearContentIds = wbPlanClassifyYearContentVos.stream().map(WbPlanClassifyYearContentVo::getClassifyYearContentId).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(classifyYearContentIds)){
                return wbPlanClassifyYearVos;
            }
            //查询三级目录
            List<WbPlanClassifyYearContentThree> wbPlanClassifyYearContentThrees =wbPlanClassifyYearContentThreeMapper.selectWbPlanClassifyYearContentThreeByClassifyYearContentIds(classifyYearContentIds);

            if (CollectionUtils.isEmpty(wbPlanClassifyYearContentThrees)){
                //二级装入一级
                Map<Long, List<WbPlanClassifyYearContentVo>> mapTwo = new HashMap<>();
                wbPlanClassifyYearContentVos.forEach(item->{
                    if (mapTwo.containsKey(item.getClassifyYearId())){
                        mapTwo.get(item.getClassifyYearId()).add(item);
                    }else {
                        List<WbPlanClassifyYearContentVo> itemWbPlanClassifyYearContentVos = new ArrayList<>();
                        itemWbPlanClassifyYearContentVos.add(item);
                        mapTwo.put(item.getClassifyYearId(),itemWbPlanClassifyYearContentVos);
                    }
                });
                wbPlanClassifyYearVos.forEach(item->{
                    if (mapTwo.containsKey(item.getId())){
                        item.setWbPlanClassifyYearContentVos(mapTwo.get(item.getId()));
                    }
                });

                return wbPlanClassifyYearVos;
            }
            //转换为三级返回
            List<WbPlanClassifyYearContentThreeVo> wbPlanClassifyYearContentThreeVos = BeanUtil.copyToList(wbPlanClassifyYearContentThrees, WbPlanClassifyYearContentThreeVo.class);
            //三级装入二级
            Map<Long, List<WbPlanClassifyYearContentThreeVo>> mapThree = new HashMap<>();
            wbPlanClassifyYearContentThreeVos.forEach(item->{
                if (mapThree.containsKey(item.getClassifyYearContentId())){
                    mapThree.get(item.getClassifyYearContentId()).add(item);
                }else {
                    List<WbPlanClassifyYearContentThreeVo> itemWbPlanClassifyYearContentThreeVos = new ArrayList<>();
                    itemWbPlanClassifyYearContentThreeVos.add(item);
                    mapThree.put(item.getClassifyYearContentId(),itemWbPlanClassifyYearContentThreeVos);
                }
            });
            wbPlanClassifyYearContentVos.forEach(item->{
                if (mapThree.containsKey(item.getClassifyYearContentId())){
                    item.setWbPlanClassifyYearContentThreeVos(mapThree.get(item.getClassifyYearContentId()));
                }
            });
            //二级装入一级
            Map<Long, List<WbPlanClassifyYearContentVo>> mapTwo = new HashMap<>();
            wbPlanClassifyYearContentVos.forEach(item->{
                if (mapTwo.containsKey(item.getClassifyYearId())){
                    mapTwo.get(item.getClassifyYearId()).add(item);
                }else {
                    List<WbPlanClassifyYearContentVo> itemWbPlanClassifyYearContentVos = new ArrayList<>();
                    itemWbPlanClassifyYearContentVos.add(item);
                    mapTwo.put(item.getClassifyYearId(),itemWbPlanClassifyYearContentVos);
                }
            });
            wbPlanClassifyYearVos.forEach(item->{
                if (mapTwo.containsKey(item.getId())){
                    item.setWbPlanClassifyYearContentVos(mapTwo.get(item.getId()));
                }
            });
            return wbPlanClassifyYearVos;
        }
    }

    /**
     * 批量查询所有年检目录
     * @param
     * @return
     */
    @Override
    public List<WbPlanClassifyYearVo> allListByCreateBy() {
        {
            List<WbPlanClassifyYearVo> wbPlanClassifyYearVos = new ArrayList<>();
            SysUser user = SecurityUtils.getLoginUser().getUser();

            //一级目录
            List<WbPlanClassifyYear> wbPlanClassifyYears =wbPlanClassifyYearMapper.selectWbPlanClassifyYearListByCreateBy(user.getUserId());
            //校验
            if (CollectionUtils.isEmpty(wbPlanClassifyYears)){
                return wbPlanClassifyYearVos;
            }
            //转换一级返回
            wbPlanClassifyYearVos = BeanUtil.copyToList(wbPlanClassifyYears, WbPlanClassifyYearVo.class);
            //校验
            List<Long> WbPlanClassifyYearIds = wbPlanClassifyYears.stream().map(WbPlanClassifyYear::getId).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(WbPlanClassifyYearIds)){
                return wbPlanClassifyYearVos;
            }
            //查询二级目录
            List<WbPlanClassifyYearContent> wbPlanClassifyYearContents=wbPlanClassifyYearContentMapper.selectWbPlanClassifyYearContentByClassifyYearIds(WbPlanClassifyYearIds);
            if (CollectionUtils.isEmpty(wbPlanClassifyYearContents)){
                return wbPlanClassifyYearVos;
            }
            //转换为二级返回
            List<WbPlanClassifyYearContentVo> wbPlanClassifyYearContentVos = BeanUtil.copyToList(wbPlanClassifyYearContents, WbPlanClassifyYearContentVo.class);


            List<Long> classifyYearContentIds = wbPlanClassifyYearContentVos.stream().map(WbPlanClassifyYearContentVo::getClassifyYearContentId).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(classifyYearContentIds)){
                return wbPlanClassifyYearVos;
            }
            //查询三级目录
            List<WbPlanClassifyYearContentThree> wbPlanClassifyYearContentThrees =wbPlanClassifyYearContentThreeMapper.selectWbPlanClassifyYearContentThreeByClassifyYearContentIds(classifyYearContentIds);

            if (CollectionUtils.isEmpty(wbPlanClassifyYearContentThrees)){
                //二级装入一级
                Map<Long, List<WbPlanClassifyYearContentVo>> mapTwo = new HashMap<>();
                wbPlanClassifyYearContentVos.forEach(item->{
                    if (mapTwo.containsKey(item.getClassifyYearId())){
                        mapTwo.get(item.getClassifyYearId()).add(item);
                    }else {
                        List<WbPlanClassifyYearContentVo> itemWbPlanClassifyYearContentVos = new ArrayList<>();
                        itemWbPlanClassifyYearContentVos.add(item);
                        mapTwo.put(item.getClassifyYearId(),itemWbPlanClassifyYearContentVos);
                    }
                });
                wbPlanClassifyYearVos.forEach(item->{
                    if (mapTwo.containsKey(item.getId())){
                        item.setWbPlanClassifyYearContentVos(mapTwo.get(item.getId()));
                    }
                });

                return wbPlanClassifyYearVos;
            }
            //转换为三级返回
            List<WbPlanClassifyYearContentThreeVo> wbPlanClassifyYearContentThreeVos = BeanUtil.copyToList(wbPlanClassifyYearContentThrees, WbPlanClassifyYearContentThreeVo.class);
            //三级装入二级
            Map<Long, List<WbPlanClassifyYearContentThreeVo>> mapThree = new HashMap<>();
            wbPlanClassifyYearContentThreeVos.forEach(item->{
                if (mapThree.containsKey(item.getClassifyYearContentId())){
                    mapThree.get(item.getClassifyYearContentId()).add(item);
                }else {
                    List<WbPlanClassifyYearContentThreeVo> itemWbPlanClassifyYearContentThreeVos = new ArrayList<>();
                    itemWbPlanClassifyYearContentThreeVos.add(item);
                    mapThree.put(item.getClassifyYearContentId(),itemWbPlanClassifyYearContentThreeVos);
                }
            });
            wbPlanClassifyYearContentVos.forEach(item->{
                if (mapThree.containsKey(item.getClassifyYearContentId())){
                    item.setWbPlanClassifyYearContentThreeVos(mapThree.get(item.getClassifyYearContentId()));
                }
            });
            //二级装入一级
            Map<Long, List<WbPlanClassifyYearContentVo>> mapTwo = new HashMap<>();
            wbPlanClassifyYearContentVos.forEach(item->{
                if (mapTwo.containsKey(item.getClassifyYearId())){
                    mapTwo.get(item.getClassifyYearId()).add(item);
                }else {
                    List<WbPlanClassifyYearContentVo> itemWbPlanClassifyYearContentVos = new ArrayList<>();
                    itemWbPlanClassifyYearContentVos.add(item);
                    mapTwo.put(item.getClassifyYearId(),itemWbPlanClassifyYearContentVos);
                }
            });
            wbPlanClassifyYearVos.forEach(item->{
                if (mapTwo.containsKey(item.getId())){
                    item.setWbPlanClassifyYearContentVos(mapTwo.get(item.getId()));
                }
            });
            return wbPlanClassifyYearVos;
        }
    }

    @Override
    @Transactional
    public int deleteWbPlanClassifyByPlanId(Long planId) {
        WbPlanClassifyYear wbPlanClassifyYear = new WbPlanClassifyYear();
        wbPlanClassifyYear.setPlanId(planId);
        List<WbPlanClassifyYear> wbPlanClassifyYears = wbPlanClassifyYearMapper.selectWbPlanClassifyYearList(wbPlanClassifyYear);
        //一级id
        List<Long> longs = wbPlanClassifyYears.stream().map(WbPlanClassifyYear::getId).collect(Collectors.toList());
        if (longs.size()==0){
            return 1;
        }
        //根据一级id查询二级目录
        List<WbPlanClassifyYearContent> wbPlanClassifyYearContents = wbPlanClassifyYearContentMapper.selectWbPlanClassifyYearContentByClassifyYearIds(longs);
        List<Long> classifyYearContentIds = wbPlanClassifyYearContents.stream().map(WbPlanClassifyYearContent::getClassifyYearContentId).collect(Collectors.toList());
         if (!CollectionUtils.isEmpty(classifyYearContentIds)){
             //删除二级
             wbPlanClassifyYearContentService.deleteWbPlanClassifyYearContentByClassifyYearContentIds(classifyYearContentIds.toArray(Long[]::new));
         }

        //删一级
        return wbPlanClassifyYearMapper.deleteWbPlanClassifyYearByIds(longs.toArray(Long[]::new));
    }
}
